package com.ruicar.afs.cloud.apply.channel.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.apply.channel.condition.*;
import com.ruicar.afs.cloud.apply.channel.entity.BlacklistTemp;
import com.ruicar.afs.cloud.apply.channel.entity.ChannelAuthorizeRegionTemp;
import com.ruicar.afs.cloud.apply.channel.entity.ChannelListingCarTempVo;
import com.ruicar.afs.cloud.apply.channel.feign.ApplyUseChannelFeign;
import com.ruicar.afs.cloud.apply.channel.mq.ApplyFileToChannelSender;
import com.ruicar.afs.cloud.apply.common.dto.ChannelInfoDTO;
import com.ruicar.afs.cloud.apply.common.dto.ComAttachmentFileDto;
import com.ruicar.afs.cloud.apply.common.feign.Apply2ChannelFeign;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConfig;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.apply.enums.ChannelNode;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import com.ruicar.afs.cloud.parameter.commom.condition.VehicleTreeCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruicar.afs.cloud.apply.common.utils.ApplyConstants.*;

/**
 * @Author Ice
 * @Date 2020/6/20
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/applyUserChannel")
@Api(value = "applyUserChannel", description = "进件调用渠道接口")
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
@Component
public class ApplyUseChannelController {
    private ApplyConfig config;
    private final ApplyUseChannelFeign applyUseChannelFeign;
    private final Apply2ChannelFeign apply2ChannelFeign;
    private final ComAttachmentFileService comAttachmentFileService;
    private final ApplyFileToChannelSender applyFileToChannelSender;

    @PostMapping("/getChannelId")
    @ApiOperation("获取登录账号的渠道id")
    public IResponse getChannelId() {
        JSONObject user = SecurityUtils.getUser().getUserExtInfo();
        return IResponse.success("查询成功！").setData(user.get("channelId").toString());
    }

    @PostMapping("/getChannelListingCarTemp")
    @ApiOperation("分页获取挂靠车数据")
    public IResponse getChannelListingCarTemp(@RequestBody ChannelListingCarRelTempCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getChannelListingCarTemp(condition, requestHeader);
    }

    @PostMapping(value = "/getChannelInfo")
    @ApiOperation(value = "获取挂靠信息")
    public IResponse getChannelInfo(@RequestBody AffiliationCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getChannelInfo(condition, requestHeader);
    }

    @PostMapping(value = "/deleteInfo")
    @ApiOperation(value = "根据id删除挂靠公司")
    public IResponse<Boolean> deleteInfo(@RequestBody AffiliationCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.deleteInfo(condition, requestHeader);
    }

    @PostMapping(value = "/upDateStatusByIds")
    @ApiOperation(value = "根据id更新挂靠公司状态")
    public IResponse<Boolean> upDateStatusByIds(@RequestBody JSONObject json) {
        String ids = json.getString("ids");
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.upDateStatusByIds(ids, requestHeader);
    }

    @PostMapping(value = "/upDateCaseStatusByIds")
    @ApiOperation(value = "根据id更新挂靠公司状态")
    public IResponse upDateCaseStatusByIds(@RequestBody ApplyAffiliatedToCaseCondition applyAffiliatedToCaseCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.upDateCaseStatusByIds(applyAffiliatedToCaseCondition, requestHeader);
    }

    @PostMapping("/carTempDelete")
    @ApiOperation("删除挂牌车备案")
    public IResponse carTempDelete(@RequestBody ChannelListingCarTempVo condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.carTempDelete(condition, requestHeader);
    }

    @PostMapping("/carTempEdit")
    @ApiOperation("修改渠道挂牌车备案")
    public IResponse carTempEdit(@RequestBody ChannelListingCarTempVo condition) {
        // 见证人修改
        List<ComAttachmentFile> fileList = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda().eq(ComAttachmentFile::getBusiNo, condition.getId().toString()).eq(ComAttachmentFile::getBelongNo, ChannelNode.carRecordApply.toString()));
        condition.setFileList(fileList);
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.carTempEdit(condition, requestHeader);
    }

    @PostMapping("/checkBlack")
    @ApiOperation("校验黑名单")
    public Object checkBlack(@RequestBody BlacklistTemp blacklistTemp) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.checkBlack(blacklistTemp, requestHeader);
    }


    @PostMapping("/channelUpAllByIds")
    @ApiOperation(value = "批量通过ids停用挂靠车备案信息")
    @Transactional
    public IResponse<Boolean> channelUpAllByIds(@RequestBody JSONObject json) {
        String ids = json.getString("ids");
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.carTempUpAllByIds(ids, requestHeader);
    }

    @PostMapping("/carsave")
    @ApiOperation("保存渠道挂牌车备案")
    @Transactional
    public IResponse carsave(@RequestBody ChannelListingCarTempVo condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.carTempSave(condition, requestHeader);
    }

    @PostMapping("/getCarTempById")
    @ApiOperation("根据id查询挂牌车信息")
    public IResponse getCarTempById(@RequestBody ChannelListingCarTempVo condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getCarTempById(condition, requestHeader);
    }

    @Transactional
    @PostMapping("/channelToExamine")
    @ApiOperation(value = "进件调用渠道挂牌车审核")
    public IResponse channelToExamine(@RequestBody ChannelListingCarTempVo carRelTemp) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.channelToExamine(carRelTemp, requestHeader);
    }

    @PostMapping(value = "/getBaseInfo")
    @ApiOperation(value = "获取合作商")
    public IResponse getBaseInfo(@RequestBody ChannelBaseInfoCondition condition) {
        //截取字符串
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("clientId", config.getCaseClientId());
        headers.put("clientSecret", config.getCaseClientSecret());
        return apply2ChannelFeign.getBaseInfo(condition, headers);
    }

    @PostMapping("/getChannelWitnessListData")
    @ApiOperation(value = "分页查询见证人数据列表")
    public IResponse getChannelWitnessListData(@RequestBody ChannelWitnessInfoTempCondition wirness) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getChannelWitnessListData(wirness, requestHeader);
    }

    @PostMapping("/channelDelWitness")
    @ApiOperation(value = "根据id删除见证人")
    @Transactional
    public IResponse channelDelWitness(@RequestBody ChannelWitnessInfoTempCondition wirness) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.channelDelWitness(wirness, requestHeader);
    }

    @PostMapping("/wintnessUpAllByIds")
    @ApiOperation(value = "根据id批量停用见证人")
    @Transactional
    public IResponse wintnessUpAllByIds(@RequestBody WitnessExpandCondition wirness) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.wintnessUpAllByIds(wirness, requestHeader);
    }

    @PostMapping("/getWitnessById")
    @ApiOperation(value = "根据id 查询见证人信息")
    @Transactional
    public IResponse getWitnessById(@RequestBody ChannelWitnessInfoTempCondition witness) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getWitnessById(witness, requestHeader);
    }

    @PostMapping("/witnessSave")
    @ApiOperation(value = "进件保存见证人")
    @Transactional
    public IResponse witnessSave(@RequestBody ChannelWitnessInfoTempCondition witness) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.witnessSave(witness, requestHeader);
    }

    @PostMapping("/addAffiliationInfo")
    @ApiOperation(value = "进件添加第三方挂靠")
    @Transactional
    public IResponse addAffiliationInfo(@RequestBody AffiliationCondition affiliationCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.addAffiliationInfo(affiliationCondition, requestHeader);
    }

    @PostMapping("/addNetCarAffiliationInfo")
    @ApiOperation(value = "进件添加网约车挂靠")
    @Transactional
    public IResponse addNetCarAffiliationInfo(@RequestBody AffiliationCondition affiliationCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.addNetCarAffiliationInfo(affiliationCondition, requestHeader);
    }

    @PostMapping("/upDateAffiliationInfo")
    @ApiOperation(value = "进件更新挂靠")
    @Transactional
    public IResponse upDateAffiliationInfo(@RequestBody AffiliationCondition affiliationCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        if (affiliationCondition.getSaveOrCommit().equals(ApplyConstants.SUBMIT)) {
            List<ComAttachmentFile> comAttachmentFileList = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda().eq(ComAttachmentFile::getBusiNo, affiliationCondition.getId().toString()));
            if (comAttachmentFileList.size() > 0) {
                ComAttachmentFileDto comAttachmentFileDto=new ComAttachmentFileDto();
                comAttachmentFileDto.setComAttachmentFiles(comAttachmentFileList);
                    AfsTransEntity<ComAttachmentFileDto> transEntity = new AfsTransEntity<>();
                    transEntity.setTransCode(MqTransCode.AFS_POS_APPLY_CHANNEL_CHANNEL_SEND_FILE_TO_CHANNEL);
                    transEntity.setData(comAttachmentFileDto);
                    log.info("进件挂靠影像信息……", transEntity);
                    applyFileToChannelSender.pushFileToChannel(transEntity);
            }
        }
        return applyUseChannelFeign.upDateAffiliationInfo(affiliationCondition, requestHeader);
    }

    @PostMapping("/getAffiliationInfoById")
    @ApiOperation(value = "根据id获取挂靠信息")
    @Transactional
    public IResponse getAffiliationInfoById(@RequestBody AffiliationCondition affiliationCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getAffiliationInfoById(affiliationCondition, requestHeader);
    }

    @PostMapping("/getAffiliationInfo")
    @ApiOperation(value = "根据id获取启用挂靠信息")
    @Transactional
    public IResponse getAffiliationInfo(@RequestBody AffiliationCondition affiliationCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getAffiliationInfo(affiliationCondition, requestHeader);
    }

    @PostMapping("/examineNewThird")
    @ApiOperation(value = "审核新增第三方挂靠")
    @Transactional
    public IResponse examineNewThird(@RequestBody AffiliationCondition affiliationCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.examineNewThird(affiliationCondition, requestHeader);
    }

    @PostMapping("/examineThird")
    @ApiOperation(value = "审核修改第三方挂靠")
    @Transactional
    public IResponse examineThird(@RequestBody AffiliationCondition affiliationCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.examineThird(affiliationCondition, requestHeader);
    }

    @PostMapping("/saveThirdOrNetCar")
    @ApiOperation(value = "审核修改第三方挂靠")
    @Transactional
    public IResponse saveThirdOrNetCar(@RequestBody ApplyAffiliatedToCaseCondition applyAffiliatedToCaseCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.saveThirdOrNetCar(applyAffiliatedToCaseCondition, requestHeader);
    }

    @PostMapping("/getMainAreaById")
    @ApiOperation(value = "获取地址")
    @Transactional
    public IResponse getMainAreaById(@RequestBody ChannelAuthorizeRegionTemp channelAuthorizeRegionTemp) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getMainAreaById(channelAuthorizeRegionTemp, requestHeader);
    }

    @PostMapping("/witnessEdit")
    @ApiOperation(value = "进件修改见证人")
    @Transactional
    public IResponse witnessEdit(@RequestBody ChannelWitnessInfoTempCondition witness) {
        // 查询见证人的数据影像信息
        List<ComAttachmentFile> fileList = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda().eq(ComAttachmentFile::getBusiNo, witness.getId().toString()).eq(ComAttachmentFile::getBelongNo, ChannelNode.witnessApply.toString()));
        witness.setFileList(fileList);
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.witnessEdit(witness, requestHeader);
    }

    @PostMapping("/witnessSaveBlack")
    @ApiOperation(value = "进件见证人申请黑名单")
    @Transactional
    public IResponse witnessSaveBlack(@RequestBody BlacklistTempCondition black) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.witnessSaveBlack(black, requestHeader);
    }

    @PostMapping("/witnessCheckRole")
    @ApiOperation(value = "进件见证人停用查询角色权限和正在见证中的合同")
    @Transactional
    public Object witnessCheckRole(@RequestBody JSONObject json) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.witnessCheckRole(json, requestHeader);
    }

    @PostMapping("/saveBlack")
    @ApiOperation(value = "进件挂靠申请黑名单")
    @Transactional
    public IResponse saveBlack(@RequestBody BlacklistTempCondition black) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.witnessSaveBlack(black, requestHeader);
    }

    @PostMapping("/witnessToExamine")
    @ApiOperation(value = "进件见证人申请审核")
    @Transactional
    public IResponse witnessToExamine(@RequestBody BlacklistTempCondition black) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.witnessToExamine(black, requestHeader);
    }

    //车商部分使用渠道接口

    @GetMapping("/getAllSaleManByChannelId/{channelId}/{businessType}")
    @ApiOperation(value = "获取渠道下所有业务员")
    public IResponse getAllSaleManByChannelId(@PathVariable(value = "channelId") String channelId, @PathVariable(value = "businessType") String businessType) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getAllSaleManByChannelId(channelId, businessType, requestHeader);
    }

    @PostMapping("/bulkEditingSalesman/{salesman}/{ids}/{channelId}")
    @ApiOperation(value = "修改业务员")
    public IResponse bulkEditingSalesman(@PathVariable(value = "salesman") String salesman, @PathVariable(value = "ids") String[] ids, @PathVariable(value = "channelId") String channelId) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.bulkEditingSalesmanFromApply(salesman, ids, channelId, requestHeader);
    }

    @PostMapping("/getCommomCarDealerByCondition")
    @ApiOperation(value = "多条件分页获取普通车商")
    public IResponse getCommonCarDealerList(@RequestBody CommonCarDealerCondition carDealerCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getCommonCarDealerList(carDealerCondition, requestHeader);
    }


    @GetMapping("/disableCarDealerFromApply/{ids}/{channelId}/{status}")
    @ApiOperation(value = "从进件端停用普通车商")
    public IResponse disableCarDealerFromApply(@PathVariable String[] ids, @PathVariable String channelId,@PathVariable String status) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.disableCarDealerFromApply(ids, channelId,status, requestHeader);
    }

    @GetMapping("/disableCarDealerFromCase/{ids}/{channelId}")
    @ApiOperation(value = "从进件端停用案件端普通车商")
    public IResponse disableCarDealerFromCase(@PathVariable String[] ids, @PathVariable String channelId) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getCaseClientId());
        requestHeader.put("clientSecret", config.getCaseClientSecret());
        return apply2ChannelFeign.disableCarDealerFromCase(ids, channelId, requestHeader);
    }

    @GetMapping("/bulkEditingSalesmanFromCase/{salesman}/{ids}/{channelId}")
    @ApiOperation(value = "批量修改业务员")
    public IResponse bulkEditingSalesmanFromCase(@PathVariable String salesman, @PathVariable String[] ids, @PathVariable String channelId) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getCaseClientId());
        requestHeader.put("clientSecret", config.getCaseClientSecret());
        return apply2ChannelFeign.bulkEditingSalesman(salesman, ids, channelId, requestHeader);
    }

    @GetMapping("/getChannelsByCardealerId/{id}")
    @ApiOperation(value = "根据车商id查询合作商信息")
    public IResponse getChannelsByCardealerId(@PathVariable String id) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getChannelsByCardealerId(id, requestHeader);
    }

    @GetMapping("/delCommonCarDealerFromApply/{id}/{channelId}")
    @ApiOperation(value = "从进件端删除普通车商")
    public IResponse delCommonCarDealerFromApply(@PathVariable String id, @PathVariable String channelId) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.deleteCommonCardealerById(id, channelId, requestHeader);
    }

    @PostMapping("/addCarDealer")
    @ApiOperation(value = "增加普通车商")
    public IResponse addCarDealer(@RequestBody CommonCarDealerCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        //同步影像件资料
        return applyUseChannelFeign.addCarDealer(condition, requestHeader);
    }

    @PostMapping("/addCarDealerToCase")
    @ApiOperation(value = "增加普通车商到案件服务")
    public IResponse addCarDealerToCase(@RequestBody CommonCarDealerCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getCaseClientId());
        requestHeader.put("clientSecret", config.getCaseClientSecret());
        return apply2ChannelFeign.addCarDealerToCase(condition, requestHeader);
    }

    @PostMapping("/editCarDealer")
    @ApiOperation(value = "编辑普通车商")
    public IResponse editCarDealer(@RequestBody CommonCarDealerCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        //同步影像件资料
        List<ComAttachmentFile> fileList =comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                .eq(ComAttachmentFile::getBusiNo,condition.getCommonCarDealer().getId().toString())
                .eq(ComAttachmentFile::getBelongNo,condition.getChannelCoopeCardealerTemp().getChannelId().toString()));
        if(CollectionUtil.isNotEmpty(fileList)){
            applyUseChannelFeign.syncFileToChannel(fileList, requestHeader);
        }
        return applyUseChannelFeign.editCarDealer(condition, requestHeader);
    }

    @GetMapping("/getcommonCarDealerInfoById/{id}")
    @ApiOperation(value = "根据普通车商id获取车商信息")
    public IResponse getcommonCarDealerInfoById(@PathVariable(value = "id") String id) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getcommonCarDealerInfoById(id, requestHeader);
    }

    @RequestMapping(value = "/getMaiBrandById/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "根据渠道id获取主营品牌信息")
    public IResponse getMaiBrandById(@PathVariable String id) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getMaiBrandById(id, requestHeader);
    }


    @PostMapping(value = "/getChannelBaseInfo/{id}")
    @ApiOperation(value = "根据渠道id获取渠道信息")
    public IResponse getChannelBaseInfo(@PathVariable String id) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getInfoById(id, requestHeader);
    }

    //直营车商的接口
    @GetMapping("/disableDirectCarDealerFromApply/{ids}/{channelId}/{status}")
    @ApiOperation(value = "禁用进件端直营车商")
    public IResponse disableDirectCarDealerFromApply(@PathVariable String[] ids, @PathVariable String channelId, @PathVariable String status) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.disableDirectCarDealerFromApply(ids, channelId,status, requestHeader);
    }

    @GetMapping("/disableDirectCarDealerFromApplyToCase/{ids}/{channelId}")
    @ApiOperation(value = "从进件端停用案件端直营车商")
    public IResponse disableDirectCarDealerFromApplyToCase(@PathVariable String[] ids, @PathVariable String channelId) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getCaseClientId());
        requestHeader.put("clientSecret", config.getCaseClientSecret());
        return apply2ChannelFeign.disableDirectCarDealerFromApply(ids, channelId, requestHeader);
    }

    @DeleteMapping("/deleteDirectCardealerByIdFromApply/{channelId}/{id}")
    @ApiOperation(value = "删除进件端直营车商")
    public IResponse deleteDirectCardealerByIdFromApply(@PathVariable String channelId, @PathVariable String id) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.deleteDirectCardealerByIdFromApply(channelId, id, requestHeader);
    }

    @PostMapping("/bulkEditingCustomerManagerFromApplyToChannel")
    @ApiOperation(value = "批量修改渠道端直营车商城市经理")
    public IResponse bulkEditingCustomerManagerFromApplyToChannel(@RequestBody ChangeSalesmanCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.bulkEditingCustomerManager(condition, requestHeader);
    }

    @GetMapping("/bulkEditingCustomerManagerFromApplyToCase/{customerManager}/{channelId}/{ids}/{type}")
    @ApiOperation(value = "批量修改案件端直营车商城市经理")
    public IResponse bulkEditingCustomerManagerFromApplyToCase(@PathVariable String customerManager, @PathVariable String channelId, @PathVariable String[] ids, @PathVariable String type) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getCaseClientId());
        requestHeader.put("clientSecret", config.getCaseClientSecret());
        return apply2ChannelFeign.bulkEditingCustomerManagerFromApply(customerManager, channelId, ids, type, requestHeader);
    }


    @PostMapping("/getAllCardealerNameByChannelId")
    @ApiOperation(value = "获取合作商下所有车商名称")
    public IResponse getAllCardealerNameByChannelId(@RequestBody CommonCarDealerCondition carDealerCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getAllCardealerNameByChannelId(carDealerCondition, requestHeader);
    }

    @PostMapping("/getDirectCarDealerListByCondition")
    @ApiOperation(value = "多条件分页获取直营车商")
    public IResponse getDirectCarDealerList(@RequestBody CommonCarDealerCondition carDealerCondition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getDirectCarDealerList(carDealerCondition, requestHeader);
    }

    @RequestMapping(value = "/saveDirectCarDealer", method = RequestMethod.POST)
    @ApiOperation(value = "保存直营车商申请信息")
    public IResponse saveDirectCarDealer(@RequestBody DirectCarDealerCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.saveDirectCarDealer(condition, requestHeader);
    }

    @RequestMapping(value = "/stagingSaveDirectCarDealer", method = RequestMethod.POST)
    @ApiOperation(value = "暂存直营车商申请信息")
    public IResponse stagingSaveDirectCarDealer(@RequestBody DirectCarDealerCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.stagingSaveDirectCarDealer(condition, requestHeader);
    }

    @RequestMapping(value = "/updateDirectCarDealer", method = RequestMethod.POST)
    @ApiOperation(value = "更新直营车商信息")
    public IResponse updateDirectCarDealer(@RequestBody DirectCarDealerCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        //同步影像件资料
        List<ComAttachmentFile> fileList =comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                .eq(ComAttachmentFile::getBusiNo,condition.getChannelBaseInfoTemp().getId().toString())
                .eq(ComAttachmentFile::getBelongNo, condition.getChannelCoopeCardealerTemp().getChannelId().toString()));
        if(CollectionUtil.isNotEmpty(fileList)){
            applyUseChannelFeign.syncFileToChannel(fileList, requestHeader);
        }
        return applyUseChannelFeign.updateDirectCarDealer(condition, requestHeader);
    }

    @PostMapping("/savaInvoiceInfo")
    @ApiOperation(value = "保存开票信息")
    public IResponse savaInvoiceInfo(@RequestBody DirectCarDealerCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.savaInvoiceInfo(condition, requestHeader);
    }

    @Transactional
    @RequestMapping(value = "/saveAccountInfo", method = RequestMethod.POST)
    @ApiOperation(value = "保存收款账号数据")
    public IResponse saveAccountInfo(@RequestBody ChannelAccountInfoCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.saveAccountInfo(condition, requestHeader);
    }

    @PostMapping(value = "/getAreaListCondition")
    @ApiOperation(value = "多条件分页获取直营车商展业")
    public IResponse getAreaListCondition(@RequestBody DirectCarDealerCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getAreaListCondition(condition, requestHeader);
    }

    @GetMapping(value = "/getAccountInfoByChannelId/{channelId}")
    @ApiOperation(value = "根据渠道id查询账户信息")
    public IResponse getAccountInfoByChannelId(@PathVariable String channelId) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getAccountInfoByChannelId(channelId, requestHeader);
    }

    @PostMapping("/checkCooper")
    @ApiOperation(value = "验证直营车商和合作商之间的关系")
    public IResponse checkCooper(@RequestBody DirectCarDealerCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.checkCooper(condition, requestHeader);
    }

    @RequestMapping(value = "/getRiskById/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "根据渠道id获取风控信息")
    public IResponse getRiskById(@PathVariable String id) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getRiskById(id, requestHeader);
    }

    @RequestMapping(value = "/getQuotaInfoById/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "根据渠道id获取保证金信息")
    public IResponse getQuotaInfoById(@PathVariable String id) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getQuotaInfoById(id, requestHeader);
    }

    @RequestMapping(value = "/getMainAreaById/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "根据渠道id获取授权区域信息")
    public IResponse getMainAreaById(@PathVariable String id) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getMainAreaById(id, requestHeader);
    }

    @RequestMapping(value = "/getVehicleById/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "根据渠道id获取授权车型信息")
    public IResponse getVehicleById(@PathVariable String id) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getVehicleById(id, requestHeader);
    }

    @RequestMapping(value = "/getAccountInfo", method = RequestMethod.POST)
    @ApiOperation(value = "多条件分页获取收款账号信息")
    public IResponse getAccountInfo(@RequestBody DirectCarDealerCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getAccountInfo(condition, requestHeader);
    }

    @RequestMapping(value = "/deleteAccount/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除收款账号信息")
    public IResponse<Boolean> deleteAccount(@PathVariable String[] ids) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.deleteAccount(ids, requestHeader);
    }

    @Transactional
    @RequestMapping(value = "/updateAccountInfo", method = RequestMethod.POST)
    @ApiOperation(value = "修改收款账号数据")
    public IResponse updateAccountInfo(@RequestBody ChannelAccountInfoCondition condition) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.updateAccountInfo(condition, requestHeader);
    }

    @GetMapping(value = "/changeAccountStatus/{channelId}")
    @ApiOperation(value = "启用车商收款账号")
    public IResponse changeAccountStatus(@PathVariable String channelId) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.changeAccountStatus(channelId, requestHeader);
    }

    @GetMapping("/getAreaByChannelId/{channelId}/{businessType}")
    @ApiOperation(value = "根据渠道id获取授权区域")
    public IResponse getAreaByChannelId(@PathVariable String channelId, @PathVariable String businessType) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getAreaByChannelId(channelId, businessType, requestHeader);
    }

    @Transactional
    @RequestMapping(value = "/getQuotaListByChannelId", method = RequestMethod.POST)
    @ApiOperation(value = "获取渠道保证金")
    public IResponse getQuotaListByChannelId(@RequestBody ChannelInfoDTO dto) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getCaseClientId());
        requestHeader.put("clientSecret", config.getCaseClientSecret());
        IResponse iResponse = apply2ChannelFeign.getQuotaListByChannelId(dto, requestHeader);
        List<ChannelQuotaCondition> quotaPutList = new ArrayList<>();
        List<ChannelQuotaCondition> quotaBeforeList = new ArrayList<>();
        if (CODE_SUCCESS.equals(iResponse.getCode())) {
            List<ChannelQuotaCondition> list = (List<ChannelQuotaCondition>) iResponse.getData();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(list.get(i));
                    ChannelQuotaCondition quota = (ChannelQuotaCondition) JSONObject.toJavaObject(jsonObject, ChannelQuotaCondition.class);
                    if (QUOTA_TYPE_ONE.equals(quota.getQuotaType())) {
                        ChannelQuotaCondition condition = new ChannelQuotaCondition();
                        condition.setBusinessType(quota.getBusinessType());
                        condition.setOccupiedQuota(quota.getOccupiedQuota());
                        condition.setQuotaType(quota.getQuotaType());
                        condition.setSurplusQuota(quota.getSurplusQuota());
                        quotaPutList.add(condition);
                    } else {
                        ChannelQuotaCondition condition = new ChannelQuotaCondition();
                        condition.setBusinessType(quota.getBusinessType());
                        condition.setOccupiedQuota(quota.getOccupiedQuota());
                        condition.setQuotaType(quota.getQuotaType());
                        condition.setSurplusQuota(quota.getSurplusQuota());
                        quotaBeforeList.add(condition);
                    }
                }
            }
            JSONObject json = new JSONObject();
            //签放额度
            json.put("quotaPutList", quotaPutList);
            //先放后抵额度
            json.put("quotaBeforeList", quotaBeforeList);
            return IResponse.success(json);
        }
        return iResponse;
    }

    @RequestMapping(value = "/getBrandTree", method = RequestMethod.POST)
    @ApiOperation(value = "获取品牌树状图")
    public IResponse getBrandTree(@RequestBody VehicleTreeCondition condition) {

        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getBrandTree(condition, requestHeader);

    }

    @RequestMapping(value = "/getNewVehicleById/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "根据渠道id获取新车授权车型信息")
    public IResponse getNewVehicleById(@PathVariable String id) {

        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getNewVehicleById(id, requestHeader);

    }

    @RequestMapping(value = "/getOldVehicleById/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "根据渠道id获取二手车授权车型信息")
    public IResponse getOldVehicleById(@PathVariable String id) {

        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getOldVehicleById(id, requestHeader);
    }

    @Transactional
    @PostMapping("/getListingCarParams")
    @ApiOperation(value = "进件调用渠道挂牌车附件条件")
    public IResponse getListingCarParams(@RequestBody ChannelListingCarTempVo carRelTemp) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getChannelClientId());
        requestHeader.put("clientSecret", config.getChannelClientSecret());
        return applyUseChannelFeign.getListingCarParams(carRelTemp, requestHeader);
    }

    @PostMapping(value = "/syncFileToApply")
    @ApiOperation("同步影像件到进件服务")
    public void syncFileToApply(@RequestBody List<ComAttachmentFile> fileList){

        if(CollectionUtil.isNotEmpty(fileList)){
            for (ComAttachmentFile file : fileList){
                comAttachmentFileService.remove(Wrappers.<ComAttachmentFile>query().lambda()
                        .eq(ComAttachmentFile::getBelongNo,file.getBelongNo())
                        .eq(ComAttachmentFile::getBusiNo,file.getBusiNo()));
                file.setId(null);
            }
            comAttachmentFileService.saveBatch(fileList);
        }
    }

}
